package com.slfx.idegen;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: slfx
 * @Date: 2021/7/6 16:58
 * @Describe:
 */
public class CodeGenerator {

    private static CodeGenerator INSTANCE = new CodeGenerator();

    public static CodeGenerator getInstance() {
        return INSTANCE;
    }

    final String projectPath = System.getProperty("user.dir")+"\\springboot-idgen-demo\\idgen-spring-boot-starter" ;

    // 设置作者
    final static String AUTHOR = "slfx";

    // 设置需要生成的表名
//    final static String[] TABLES = {"system_menu","system_role","system_role_menu","system_user_role","system_users"};
    final static String[] TABLES = {"guid"};

    public void generate() {

        FastAutoGenerator.create(
                "jdbc:mysql://10.8.200.56:3306/ibs_video?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8",
                "root", "tTxg3nSFtTxS")
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
                            .disableOpenDir() // 关闭打开目录
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(projectPath + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {

                    Map<OutputFile,String> pathInfo = new HashMap<>();
                    pathInfo.put(OutputFile.mapperXml, projectPath + "/src/main/resources/mapper");
                    pathInfo.put(OutputFile.service, projectPath + "/src/main/java/com/slfx/idgen/server/service");
                    pathInfo.put(OutputFile.serviceImpl, projectPath + "/src/main/java/com/slfx/idgen/server/service/impl");
                    pathInfo.put(OutputFile.controller, projectPath + "/src/main/java/com/slfx/idgen/server/controller");
                    builder.parent("com.slfx") // 设置父包名
                            .moduleName("idgen.server") // 设置父包模块名
                            .pathInfo(pathInfo); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(TABLES) // 设置需要生成的表名
                            .entityBuilder().enableLombok()
                            // 通用实体父类
//                            .superClass("com.bzl.ibs.core.mp.base.Base")
//                            .addSuperEntityColumns("id", "create_by", "create_time", "update_by", "update_time", "deleted")
                            .controllerBuilder()
                           .enableRestStyle().enableHyphenStyle()
                    ;
                })
                .templateConfig(builder -> {
                    // 配置不生成的文件
//                    builder.disable(TemplateType.CONTROLLER,TemplateType.MAPPER,TemplateType.SERVICE,TemplateType.SERVICEIMPL);
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }

    public static void main(String[] args) {
        getInstance().generate();
    }
}
